<?php
    session_start();
	require("autoload.php");
    LeanCloud\Client::initialize("f7A9x2W7MYKoWNwt6ivPqsiU-gzGzoHsz", "sRNmqE7SmB4gQwQzc0F2DjYF", "2oDAabzEXm30UCM5dtGN6W8q");
    use LeanCloud\User;
    use LeanCloud\CloudException;
    use LeanCloud\Query;
	
	$username = $_POST['username'];
	$password = $_POST['password'];
	try {
		User::logIn($username,$password);
    } catch (Exception $e) {
    	$_SESSION["login_error"] = "用户名或密码错误！";
        header("Location:loginbg.php");
        exit();
    }
	$currentUser = User::getCurrentUser();
	$roleid = $currentUser->get("role");
	$query = new Query("Rolelist");
	$rolelist = $query->get($roleid)->get("roleobject");
	//echo $currentUser->get("role");
	$session = $currentUser->getSessionToken();
    foreach($rolelist as $key => $value){
    	//echo $key;
		$_SESSION["user_role"] = $rolelist;
    	setcookie("lbx", $session, time() + 1000, "/"); 
		header("Location:".$key.".php");
		exit();
	}
    $_SESSION["login_error"] = "没有访问后台权限！";
	header("Location:loginbg.php");
?>